<?php
namespace App\Model;

use lib\Model;

class Gallery extends Model
{
    private $id;
    private $title;
    private $description;
    private $art = '';
    // 1表示公开，0表示私密
    private $status = 1;

    /**
     * 一个相册包含多个图片
     *
     * @var Image
     */
    private $imageCollection = [];
    private $created_at;
    private $updated_at;
    private $deleted_at = null;

    /**
     * 一个相册属于一个用户
     *
     * @var User
     */
    private $user;

    protected $dataDirectory = 'gallery';

    public static function fromArray(Array $array)
    {
        if (empty($array) || count($array) == 0) {
            return null;
        }

        $gallary = new Gallery();
        $gallary->id = $array['id'];
        $gallary->title = $array['title'];
        $gallary->description = $array['description'];
        $gallary->art = isset($array['art']) ? $array['art'] : '';
        $gallary->created_at = time();
        $gallary->updated_at = $gallary->created_at;
        $gallary->user = User::fromArray($array['user']);
        foreach ($array['imageCollection'] as $imageArray) {
            $image = Image::fromArray($imageArray);
            $gallary->imageCollection[] = $image;
        }
        return $gallary;
    }

    public function toArray()
    {
        $imageArrayCollection = [];
        foreach ($this->imageCollection as $image) {
            $imageArrayCollection[] = $image->toArray();
        }

        return [
            'id' => $this->id,
            'title' => $this->title,
            'description' => $this->description,
            'art' => $this->art,
            'status' => $this->status,
            'user' => $this->user->toArray(),
            'imageCollection' => $imageArrayCollection
        ];
    }

    public function addImage(Image $image)
    {
        $this->imageCollection[] = $image;
    }

    /**
     * 上传多张图片到当前相册
     */
    public function upload()
    {
        // 1. 处理上传文件，并保存
        // 2. 把图片信息加入到相册的图片集合
        $files = $_FILES['imgs'];
        $count = count($files['name']);

        $types = ['image/png', 'image/jpeg', 'image/jpg', 'image/gif'];
        $uploadDir = 'upload' . DIRECTORY_SEPARATOR;
        // 处理每一张图片
        for ($i = 0; $i < $count; $i++) {
            // 处理上传图片文件
            if ($files['error'][$i] == 0) {
                if (in_array($files['type'][$i], $types)) {
                    if (($files['size'][$i] > 0) && ($files['size'][$i] < 5 * 1024 * 1024)) {
                        $fileName = md5(mt_rand() . time() . $files['name'][$i]);
                        // 上传文件相对于public的相对路径
                        $filePath = $uploadDir . $fileName . '.' . pathinfo($files['name'][$i])['extension'];
                        $imageId = md5($filePath);
                        // 上传文件的绝对路径
                        $destination = PUBLIC_DIR . DIRECTORY_SEPARATOR . $filePath;
                        move_uploaded_file($files['tmp_name'][$i], $destination);
                        // 上传成功之后，构建图片Image对象
                        $image = new Image($imageId, $fileName, $filePath);
                        $this->addImage($image);
                    }
                }
            }
        }

        $artImg = isset($this->imageCollection[0]) ? $this->imageCollection[0] : null;
        if (!empty($artImg)) {
            $this->art = $artImg->path;
        }
    }

    public function __get($name)
    {
        if (property_exists(self::class, $name)) {
            return $this->$name;
        }
        return null;
    }

    public function __set($name, $value)
    {
        if (property_exists(self::class, $name)) {
            $this->$name = $value;
        }
    }
}
